var time = Date.now()-3000000;
findAppsLastUpdateTime(time, "false")

// 写入包名到本地文件
function writePackagesToFile(packages) {
    let filePath = "/sdcard/脚本/test_packages.txt";
    try {
        files.write(filePath, packages.join("\n"));
        toast("包名写入成功");
        log("写入的包名:", packages);
    } catch (e) {
        toast("写入失败: " + e);
    }
}



// ==================== 查询app安装时间====================
function findAppsLastUpdateTime(time, param) {
    //sleep(30000);

    var pm = context.getPackageManager()
    var appList = pm.getInstalledApplications(0)
    var packages = [];

    for (let i = 0; i < appList.size(); i++) {
        var app_ = appList.get(i)
        var appInfo = {
            appName: app_.loadLabel(pm),
            packageName: app_.packageName,

            lastUpdateTime: pm.getPackageInfo(app_.packageName, 0).lastUpdateTime
        }
        if (appInfo.lastUpdateTime > time && param == "true") {
            console.log("卸载" +appInfo.appName);
            toast("卸载" + appInfo.appName)
            shell('pm uninstall ' + appInfo.packageName, true);
            sleep(2000)

        } 
        if (appInfo.lastUpdateTime > time && param == "false") {
            console.log("写入包名" +appInfo.appName);
            toast("写入包名" + appInfo.appName)
            packages.push(appInfo.packageName)

        }

    }
    writePackagesToFile(packages)
}

